package com.sjsu.CMPE226.etl.parser;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import com.sjsu.CMPE226.etl.dataloader.NormalisedJDBCDataLoader;
import com.sjsu.CMPE226.etl.dataloader.PartionedJDBCDataLoader;
import com.sjsu.CMPE226.normalized.jdbc.data.Network;
import com.sjsu.CMPE226.normalized.jdbc.data.Provider;
import com.sjsu.CMPE226.normalized.jdbc.data.Station;

/**
 * 
 * @author Pawar Deepali
 * 
 */
public class JDBCWeatherCsvFileParser {
	List<Network> networkList = new ArrayList<Network>();
	List<Provider> providerList = new ArrayList<Provider>();
	List<Station> stationList = new ArrayList<Station>();

	public void readFile(String fileName, String databasetype) {

		Set<Object> nonDuplicateSet = new HashSet<Object>();
		Set<Object> nonDuplicateProviderSet = new HashSet<Object>();
		Set<String> nonDuplicateStationSet = new HashSet<String>();

		try {
			int count = 0, dupcount = 0;
			Scanner scanner = new Scanner(new File(fileName));
			scanner.useDelimiter("\n");
			while (scanner.hasNext()) {
				count++;
				String line = scanner.next();
				String[] tokens = line.split("[,]");
				if (count > 1) {
					Network network = new Network();
					Provider primprovider = new Provider();
					Provider secprovider = new Provider();
					Provider tertprovider = new Provider();

					setNetworkProviderValues(network, primprovider,
							secprovider, tertprovider, tokens);
					if (nonDuplicateSet.add(network)) {
						networkList.add(network);
					}
					if (nonDuplicateProviderSet.add(primprovider)) {
						providerList.add(primprovider);
					}
					if (nonDuplicateProviderSet.add(secprovider)) {
						providerList.add(secprovider);
					}
					if (nonDuplicateProviderSet.add(tertprovider)) {
						providerList.add(tertprovider);
					}
				}

				if (count > 1) {
					Station station = new Station();
					setStationValues(station, tokens);
					if (nonDuplicateStationSet.add(station.getStationId())) {
						stationList.add(station);
					} else {
						dupcount++;
					}
				}
			}
			scanner.close();
			System.out.println("No of lines" + count);
			System.out.println("No of networks" + networkList.size());
			System.out.println("No of providers" + providerList.size());
			System.out.println("No of duplicate stations not added to database"
					+ dupcount);
			if (databasetype.equalsIgnoreCase("N"))
				loadCSVDataNormalised();
			else
				loadCSVDataPartion();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void loadCSVDataPartion() {
		PartionedJDBCDataLoader PartionedJDBCDataLoader = new PartionedJDBCDataLoader();
		PartionedJDBCDataLoader.loadNetworksBatch(networkList);
		PartionedJDBCDataLoader.loadProvidersBatch(providerList);
		PartionedJDBCDataLoader.loadStationsBatch(stationList);

	}

	public void loadCSVDataNormalised() {
		NormalisedJDBCDataLoader JDBCDataLoader = new NormalisedJDBCDataLoader();
		JDBCDataLoader.loadNetworksBatch(networkList);
		JDBCDataLoader.loadProvidersBatch(providerList);
		JDBCDataLoader.loadStationsBatch(stationList);

	}

	public void setStationValues(Station station, String[] tokens) {
		if (tokens.length > 15) {
			station.setStationId(tokens[0]);
			station.setSecondaryStationId(tokens[1]);
			station.setStationName(tokens[2]);
			station.setState(tokens[3]);
			station.setCountry(tokens[4]);
			if (!tokens[5].equalsIgnoreCase("NULL"))
				station.setLatitude(Double.parseDouble(tokens[5]));
			if (!tokens[6].equalsIgnoreCase("NULL"))
				station.setLongitude(Double.parseDouble(tokens[6]));
			station.setElevation(Float.parseFloat(tokens[7]));
			station.setNetworkId(Integer.parseInt(tokens[8]));
			station.setStatus(tokens[10]);
			if (!tokens[11].isEmpty())
				station.setPrimaryProviderId(Integer.parseInt(tokens[11]));
			if (!tokens[13].isEmpty())
				station.setSecondaryProviderId(Integer.parseInt(tokens[13]));
			if (!tokens[15].isEmpty())
				station.setTertiaryProviderId(Integer.parseInt(tokens[15]));
			if (!tokens[17].isEmpty() && !tokens[17].equalsIgnoreCase(";")) {
				station.setWimsId(Integer.parseInt(tokens[17].substring(0,
						tokens[17].length() - 1)));
			}
		} else {
			System.out.println("Error in row data insufficient, skipping");
		}
	}

	public void setNetworkProviderValues(Network network,
			Provider primprovider, Provider secprovider, Provider tertprovider,
			String[] tokens) {

		for (int i = 0; i < tokens.length; i++) {
			if (!tokens[i].isEmpty() && tokens[i] != "" && tokens[i] != null
					&& tokens[i] != "null" && !tokens[i].equalsIgnoreCase(";")) {
				switch (i) {

				case 8:
					network.setNetworkId(Integer.parseInt(tokens[i]));
					break;
				case 9:
					network.setNetworkName(tokens[i]);
					break;
				case 11:
					if (Integer.parseInt(tokens[i]) > 0) {
						primprovider.setProviderId(Integer.parseInt(tokens[i]));
					}
					break;
				case 12:
					if (Integer.parseInt(tokens[i - 1]) > 0) {
						primprovider.setProviderName(tokens[i]);
					}
					break;
				case 13:
					if (Integer.parseInt(tokens[i]) > 0) {
						secprovider.setProviderId(Integer.parseInt(tokens[i]));
					}
					break;
				case 14:
					if (Integer.parseInt(tokens[i - 1]) > 0) {
						secprovider.setProviderName(tokens[i]);
					}
					break;
				case 15:
					if (Integer.parseInt(tokens[i]) > 0) {
						tertprovider.setProviderId(Integer.parseInt(tokens[i]));
					}
					break;
				case 16:
					if (Integer.parseInt(tokens[i - 1]) > 0) {
						tertprovider.setProviderName(tokens[i]);
					}
					break;
				}
			}

		}

	}
}
